package top.zhongyy.aspect;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.zhongyy.enumPackage.TouristEnum;
import top.zhongyy.pojo.BlogLog;
import top.zhongyy.service.BlogLogService;
import top.zhongyy.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;

/**
 * @Created by IntelliJ IDEA.
 * @DESC:AOP注解日志文档
 * @Author: Andy
 * @Date: 2021/3/19
 */
@Aspect
@Component
public class LogAspect {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private String url;
    private String classMethod;
    private HttpServletRequest request;
    
    @Autowired
    private BlogLogService blogLogService;
    
    // controller包下的所有类的所有方法
    @Pointcut("execution(* top.zhongyy.controller.*.*(..))")
    public void log() {
        
    }
    
    // 前置通知：在目标方法之前执行
    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
        
        //通过request拿到url和ip
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        request = attributes.getRequest();
        url = request.getRequestURL().toString();
        String ip = request.getRemoteAddr();
        // 获得方法类型和方法名
        classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        RequestLog requestLog = new RequestLog(url, ip, classMethod, args);
        // {} 占位符
        logger.info("Request : {}", requestLog);
    }
    
    
    @After("log()")
    public void doAfter() {
        try {
            blogLogService.addLog(url, classMethod, request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        logger.info("--------doAfter--------");
    }
    
    // 后置通知：在目标方法之后执行
    @AfterReturning(returning = "result", pointcut = "log()")
    public void doAfterReturn(Object result) {
        logger.info("Result: {}", result);
    }
    
    private class RequestLog {
        private String url;
        private String ip;
        private String classMethod;
        private Object[] args;  //对象数组
        
        public RequestLog(String url, String ip, String classMethod, Object[] args) {
            this.url = url;
            this.ip = ip;
            this.classMethod = classMethod;
            this.args = args;
        }
        
        @Override
        public String toString() {
            return "RequestLog{" +
                    "url='" + url + '\'' +
                    ", ip='" + ip + '\'' +
                    ", classMethod='" + classMethod + '\'' +
                    ", args=" + Arrays.toString(args) +
                    '}';
        }
    }
}
